package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.rank.UnionActivityRankListModel;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.CommonActivityMgr;
import com.yanqu.road.server.manger.activity.CommonActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_COMMON_ACTIVITY_UNION_RANK, desc = "商会活动积分排行榜")
public class CommonActivityUnionRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityUnionRankListReqMsg reqMsg = CommonActivityProto.CommonActivityUnionRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        CommonActivityProto.CommonActivityUnionRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityUnionRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        ActivityInfo openActivityInfo = CommonActivityMgr.getOpenCommonActivityInfo(activityId);
        if (null != openActivityInfo) {
            UnionActivityRankListModel rankListModel = CommonActivityRankMgr.getUnionRankListModel(openActivityInfo.getActivityId());
            if (null != rankListModel) {
                respMsg.setRankList(rankListModel.getRankListMsg());
                UnionActivityConditionData unionActivityConditionData = CommonActivityRankMgr.getUnionActivityConditionData(openActivityInfo.getActivityId(),
                        player.getUserId());
                if(null != unionActivityConditionData) {
                    respMsg.setMyRank(rankListModel.getMyRank(unionActivityConditionData.getUnionUid()));
                    respMsg.setValue(unionActivityConditionData.getValue().toString());
                }
            }
        }
        player.sendPacket(Protocol.U_COMMON_ACTIVITY_UNION_RANK, respMsg);
    }
}
